from uliweb.orm import *
#from uliweb.contrib.auth.models import 'user'
#from userman.models import UserProfile
import datetime

class Todo(Model):
#    __table_args__ = dict(mysql_charset='utf8')
    
    title = Field(str, max_length=200)
    content = Field(TEXT, default='')
    owner = Reference('user', collection_name="owner_todos")
    users = ManyToMany('user', collection_name='todos')
    due_date = Field(datetime.datetime)
    create_date = Field(datetime.datetime, auto_now_add=True)
    finish_date = Field(datetime.datetime, auto_now_add=True)
    modify_date = Field(datetime.datetime, auto_now=True)
    modify_user = Reference('user', collection_name="modify_user_todos")
    finished = Field(bool, default=False)
    public = Field(bool)
    enable_vote = Field(bool)
    deleted = Field(bool)
    
#    @classmethod
#    def OnInit(cls):
#        Index('my_indx', cls.c.title, cls.c.owner, unique=True)
        
    def get_tags(self):
        s = []
        for t in self.tags.all():
            s.append(t.name)
        return s
    
    def save_tags(self, tags):
        s = []
        for tag in list(self.tags.all()):
            tag.counter -= 1
            tag.save()
            if not tag.counter:
                tag.delete()
                
        self.tags.clear()
                
        for t in tags:
            tag = Tag.get(Tag.c.name==t)
            if not tag:
                tag = Tag(name=t, todo=self)
            tag.counter += 1
            tag.save()
            self.tags.add(tag)
                
    def comment(self, content, user):
        a = Comment(user=user, content=content, todo=self)
        a.save()
        return a
    
class Comment(Model):
    user = Reference('user')
    content = Field(TEXT)
    date = Field(datetime.datetime, auto_now_add=True)
    todo = Reference(Todo, collection_name='comments')

class TodoFile(Model):
    filename = Field(CHAR, max_length=200)
    todo = Reference(Todo, collection_name='files')
    user = Reference('user')
    date = Field(datetime.datetime, auto_now_add=True)
    size = Field(int)
    
class TodoLog(Model):
    user = Reference('user')
    date = Field(datetime.datetime, auto_now_add=True)
    content = Field(TEXT)
    
class CommentFile(Model):
    filename = Field(CHAR, max_length=200)
    comment = Reference(Comment, collection_name='files')
    user = Reference('user')
    date = Field(datetime.datetime, auto_now_add=True)
    size = Field(int)
    
class Tag(Model):
    name = Field(CHAR, max_length=64)
    todo = ManyToMany(Todo, collection_name='tags')
    counter = Field(int)

class Vote(Model):
    user = Reference('user')
    ok = Field(bool, default=False)
    todo = Reference(Todo, collection_name='votes')
    
class Message(Model):
    date = Field(datetime.datetime, auto_now_add=True)
    sender = Reference('user')
    owner = Reference('user', collection_name='messages')
    content = Field(TEXT)
    
class CheckPoint(Model):
    todo = Reference(Todo, collection_name="checkpoints")
    title = Field(str, max_length=200)
    finished = Field(bool, default=False)
    user = Reference('user')
    date = Field(datetime.datetime, auto_now=True)
  
class Diff(Model):
    todo = Reference(Todo, collection_name='diffs')
    date = Field(datetime.datetime, auto_now_add=True)
    content = Field(TEXT)
    
class Activity(Model):
    user = Reference('user')
    date = Field(datetime.datetime, auto_now_add=True)
    content = Field(TEXT)